﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using CookComputing.XmlRpc;

namespace SpacesToBloggerMigrator
{
    class SpacesEngine : IBlogEngine
    {
        private MsnSpacesMetaWeblog proxy = new MsnSpacesMetaWeblog();
        private Uri blog;
        private string username;
        private string password;
        #region IBlogEngine Members

        public void Login(string username, string password, Uri blogURL)
        {
            this.proxy.Credentials = new NetworkCredential(username, password);
            this.username = username;
            this.password = password;
            this.blog = blogURL;
        }

        public List<_Post> GetPosts()
        {
            List<_Post> result = new List<_Post>();
            Post[] posts = proxy.getRecentPosts("MyBlog", this.username, this.password, 20);
            foreach (Post p in posts)
            {
                _Post post = new _Post();
                post.Title = p.title;
                post.Content = p.description;
                post.Date = p.dateCreated;
                result.Add(post);
            }
            return result;
        }

        public bool InsertPost(_Post post)
        {
            Post p = new Post();
            p.title = post.Title;
            p.description = post.Content;
            p.dateCreated = post.Date;
            string id = this.proxy.newPost("MyBlog", this.username, this.password, p, true);
            if (!string.IsNullOrEmpty(id))
                return true;
            else
                return false;
        }

        #endregion

        #region RPCStuff
        
        [XmlRpcMissingMapping(MappingAction.Ignore)]
        struct UserBlog
        {
            public string url;
            public string blogid;
            public string blogName;
        }


        [XmlRpcMissingMapping(MappingAction.Ignore)]
        struct UserInfo
        {
            public string url;
            public string blogid;
            public string blogName;
            public string firstname;
            public string lastname;
            public string email;
            public string nickname;
        }


        [XmlRpcMissingMapping(MappingAction.Ignore)]
        struct Category
        {
            public string description;
            public string title;
        }

        [XmlRpcMissingMapping(MappingAction.Ignore)]
        struct Post
        {
            public DateTime dateCreated;
            public string description;
            public string title;
            public string postid;
            public string[] categories;
        }

        [XmlRpcUrl("https://storage.msn.com/storageservice/MetaWeblog.rpc")]
        class MsnSpacesMetaWeblog : XmlRpcClientProtocol
        {
            [XmlRpcMethod("metaWeblog.getRecentPosts")]
            public Post[] getRecentPosts(
            string blogid,
            string username,
            string password,
            int numberOfPosts)
            {
                return (Post[])this.Invoke("getRecentPosts", new object[] { blogid, username, password, numberOfPosts });
            }


            [XmlRpcMethod("metaWeblog.newPost")]
            public string newPost(
            string blogid,
            string username,
            string password,
            Post content,
            bool publish)
            {
                return (string)this.Invoke("newPost", new object[] { blogid, username, password, content, publish });
            }

            [XmlRpcMethod("blogger.deletePost")]
            public bool deletePost(
            string appKey,
            string postid,
            string username,
            string password,
            bool publish)
            {
                return (bool)this.Invoke("deletePost", new object[] { appKey, postid, username, password, publish });
            }
        }

        #endregion
    }
}